package com.wannengbang.cloudleader.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.utils.Dp2Px;
import com.wannengbang.cloudleader.utils.ScreenUtils;
import com.wannengbang.cloudleader.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Author:HWC
 */
public class InputBuyNumDialog extends DialogFragment {


    @BindView(R.id.edit_num)
    EditText editNum;
    @BindView(R.id.dialog_negative)
    TextView dialogNegative;
    @BindView(R.id.dialog_positive)
    TextView dialogPositive;
    private Dialog dialog;
    Unbinder unbinder;
    private Context mContext;
    private OnCallBackListener onCallBackListener;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_input_buy_num, null);
        unbinder = ButterKnife.bind(this, view);
        mContext = getActivity();
        initView();
        return view;
    }

    public static InputBuyNumDialog newInstance(String num) {
        Bundle args = new Bundle();
        InputBuyNumDialog fragment = new InputBuyNumDialog();
        args.putString("num", num);
        fragment.setArguments(args);
        return fragment;
    }

    //    初始化请求
    private void initView() {
        String num = getArguments().getString("num");
        editNum.setText(num);
        if (!TextUtils.isEmpty(num)) {
            editNum.setSelection(num.length());
        }
    }


    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (ScreenUtils.getScreenWidth(mContext) - Dp2Px.dp2Px(65));
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.dialog_negative, R.id.dialog_positive})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_negative:
                dismiss();
                break;
            case R.id.dialog_positive:
                String number = editNum.getText().toString();
                if (TextUtils.isEmpty(number)) {
                    ToastUtil.showShort("请输入购买数量");
                    return;
                }
                onCallBackListener.onCallBack(Integer.parseInt(number));
                dismiss();
                break;
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int num);
    }
}
